<?php

class bank extends Front_Controller {

	function __construct()
	{
		parent::__construct();
	}
	public function index(){
		$this->autoresponse();
	}
	public function autoresponse(){
	
		$path_root=FCPATH.'sherlock';

		// Récupération de la variable cryptée DATA
		$message="message=$HTTP_POST_VARS[DATA]";

		// Initialisation du chemin du fichier pathfile (à modifier)
			//   ex :
			//    -> Windows : $pathfile="pathfile=c:/repertoire/pathfile"
			//    -> Unix    : $pathfile="pathfile=/home/repertoire/pathfile"
			
		$pathfile="pathfile={$path_root}param/pathfile";

		//Initialisation du chemin de l'executable response (à modifier)
		//ex :
		//-> Windows : $path_bin = "c:/repertoire/bin/response"
		//-> Unix    : $path_bin = "/home/repertoire/bin/response"
		//

		$path_bin = $path_root.'bin/response';

		// Appel du binaire response
		$message = escapeshellcmd($message);
		$result=exec("$path_bin $pathfile $message");

		//	Sortie de la fonction : !code!error!v1!v2!v3!...!v29
		//		- code=0	: la fonction retourne les données de la transaction dans les variables v1, v2, ...
		//				: Ces variables sont décrites dans le GUIDE DU PROGRAMMEUR
		//		- code=-1 	: La fonction retourne un message d'erreur dans la variable error


		//	on separe les differents champs et on les met dans une variable tableau

		$tableau = explode ("!", $result);

		$code = $tableau[1];
		$error = $tableau[2];
		$merchant_id = $tableau[3];
		$merchant_country = $tableau[4];
		$amount = $tableau[5];
		$transaction_id = $tableau[6];
		$payment_means = $tableau[7];
		$transmission_date= $tableau[8];
		$payment_time = $tableau[9];
		$payment_date = $tableau[10];
		$response_code = $tableau[11];
		$payment_certificate = $tableau[12];
		$authorisation_id = $tableau[13];
		$currency_code = $tableau[14];
		$card_number = $tableau[15];
		$cvv_flag = $tableau[16];
		$cvv_response_code = $tableau[17];
		$bank_response_code = $tableau[18];
		$complementary_code = $tableau[19];
		$complementary_info= $tableau[20];
		$return_context = $tableau[21];
		$caddie = $tableau[22];
		$receipt_complement = $tableau[23];
		$merchant_language = $tableau[24];
		$language = $tableau[25];
		$customer_id = $tableau[26];
		$order_id = $tableau[27];
		$customer_email = $tableau[28];
		$customer_ip_address = $tableau[29];
		$capture_day = $tableau[30];
		$capture_mode = $tableau[31];
		$data = $tableau[32];


		// Initialisation du chemin du fichier de log (à modifier)
		//   ex :
		//    -> Windows : $logfile="c:/repertoire/log/logfile.txt";
		//    -> Unix    : $logfile="/home/repertoire/log/logfile.txt";
		//

		$logfile=dirname($path_root).'/log/autoreponse.txt';

		// Ouverture du fichier de log en append

		$fp=fopen($logfile, "a");

		//  analyse du code retour

		  if (( $code == "" ) && ( $error == "" ) ){
			fwrite($fp, "erreur appel response\n");
			print ("executable response non trouve $path_bin\n");
		}

		//	Erreur, sauvegarde le message d'erreur

		else if ( $code != 0 ){
			// payment was rejected, take some action, send emails, investigate, possible fraud ..
			if($order_id!="" && $order_id!=0){
				$this->db->where('order_number', $order_id);
				$this->db->update('orders', array('status'=>'Cancelled','notes'=>'Faut Transaction'));
			}
			fwrite($fp, " API call error.\n");
			fwrite($fp, "Error message :  $error\n");
		}
		else {

		// OK, Sauvegarde des champs de la réponse
			// update the status order and the shop stocks
			if($order_id!="" && $order_id!=0){
				$this->db->where('order_number', $order_id);
				$this->db->update('orders', array('status'=>'Pay','notes'=>'TXN: '.$transaction_id));
			}
		
		fwrite( $fp, "merchant_id : $merchant_id\n");
		fwrite( $fp, "merchant_country : $merchant_country\n");
		fwrite( $fp, "amount : $amount\n");
		fwrite( $fp, "transaction_id : $transaction_id\n");
		fwrite( $fp, "transmission_date: $transmission_date\n");
		fwrite( $fp, "payment_means: $payment_means\n");
		fwrite( $fp, "payment_time : $payment_time\n");
		fwrite( $fp, "payment_date : $payment_date\n");
		fwrite( $fp, "response_code : $response_code\n");
		fwrite( $fp, "payment_certificate : $payment_certificate\n");
		fwrite( $fp, "authorisation_id : $authorisation_id\n");
		fwrite( $fp, "currency_code : $currency_code\n");
		fwrite( $fp, "card_number : $card_number\n");
		fwrite( $fp, "cvv_flag: $cvv_flag\n");
		fwrite( $fp, "cvv_response_code: $cvv_response_code\n");
		fwrite( $fp, "bank_response_code: $bank_response_code\n");
		fwrite( $fp, "complementary_code: $complementary_code\n");
		fwrite( $fp, "complementary_info: $complementary_info\n");
		fwrite( $fp, "return_context: $return_context\n");
		fwrite( $fp, "caddie : $caddie\n");
		fwrite( $fp, "receipt_complement: $receipt_complement\n");
		fwrite( $fp, "merchant_language: $merchant_language\n");
		fwrite( $fp, "language: $language\n");
		fwrite( $fp, "customer_id: $customer_id\n");
		fwrite( $fp, "order_id: $order_id\n");
		fwrite( $fp, "customer_email: $customer_email\n");
		fwrite( $fp, "customer_ip_address: $customer_ip_address\n");
		fwrite( $fp, "capture_day: $capture_day\n");
		fwrite( $fp, "capture_mode: $capture_mode\n");
		fwrite( $fp, "data: $data\n");
		fwrite( $fp, "-------------------------------------------\n");
		}

		fclose ($fp);
	
	}
	
	public function response(){
		$message="message=$HTTP_POST_VARS[DATA]";
	
		// Initialisation du chemin du fichier pathfile (à modifier)
		//   ex :
		//    -> Windows : $pathfile="pathfile=c:/repertoire/pathfile";
		//    -> Unix    : $pathfile="pathfile=/home/repertoire/pathfile";
	   $ROOT=FCPATH.'sherlock/';
	   $pathfile="pathfile=".$ROOT."param/pathfile";

		// Initialisation du chemin de l'executable response (à modifier)
		// ex :
		// -> Windows : $path_bin = "c:/repertoire/bin/response";
		// -> Unix    : $path_bin = "/home/repertoire/bin/response";
		//

		$path_bin = $ROOT."bin/response";

		// Appel du binaire response
		$message = escapeshellcmd($message);
		$result=exec("$path_bin $pathfile $message");
		

		//	Sortie de la fonction : !code!error!v1!v2!v3!...!v29
		//		- code=0	: la fonction retourne les données de la transaction dans les variables v1, v2, ...
		//				: Ces variables sont décrites dans le GUIDE DU PROGRAMMEUR
		//		- code=-1 	: La fonction retourne un message d'erreur dans la variable error


		//	on separe les differents champs et on les met dans une variable tableau

		$tableau = explode ("!", $result);

		//	Récupération des données de la réponse

		$code = $tableau[1];
		$error = $tableau[2];
		$merchant_id = $tableau[3];
		$merchant_country = $tableau[4];
		$amount = $tableau[5];
		$transaction_id = $tableau[6];
		$payment_means = $tableau[7];
		$transmission_date= $tableau[8];
		$payment_time = $tableau[9];
		$payment_date = $tableau[10];
		$response_code = $tableau[11];
		$payment_certificate = $tableau[12];
		$authorisation_id = $tableau[13];
		$currency_code = $tableau[14];
		$card_number = $tableau[15];
		$cvv_flag = $tableau[16];
		$cvv_response_code = $tableau[17];
		$bank_response_code = $tableau[18];
		$complementary_code = $tableau[19];
		$complementary_info = $tableau[20];
		$return_context = $tableau[21];
		$caddie = $tableau[22];
		$receipt_complement = $tableau[23];
		$merchant_language = $tableau[24];
		$language = $tableau[25];
		$customer_id = $tableau[26];
		$order_id = $tableau[27];
		$customer_email = $tableau[28];
		$customer_ip_address = $tableau[29];
		$capture_day = $tableau[30];
		$capture_mode = $tableau[31];
		$data = $tableau[32];

		//  analyse du code retour

		  if (( $code == "" ) && ( $error == "" ) ){
			print ("<BR><CENTER>erreur appel response</CENTER><BR>");
			print ("executable response non trouve $path_bin");
			print ("<br><br><a href='".base_url()."/checkout/step_3'> return latinodances</a>");
		}

		//	Erreur, affiche le message d'erreur

		else if ( $code != 0 ){
			print ("<center><b><h2>Erreur appel API de paiement.</h2></center></b>");
			print ("<br><br><br>");
			print (" message erreur : $error <br>");
			print ("<br><br><a href='".base_url()."/checkout/step_3'> return latinodances</a>");
		}

		// OK, affichage des champs de la réponse
		else {
			
			# OK, affichage du mode DEBUG si activé
			print (" $error <br>");
				
			print("<center>\n");
			print("<H3>R&eacute;ponse manuelle du serveur SHERLOCKS</H3>\n");
			print("</center>\n");
			print("<b><h4>\n");
			print("<br><hr>\n");
			print("<br>merchant_id : $merchant_id\n");
			print("<br>merchant_country : $merchant_country\n");
			print("<br>amount : $amount\n");
			print("<br>transaction_id : $transaction_id\n");
			print("<br>transmission_date: $transmission_date\n");
			print("<br>payment_means: $payment_means\n");
			print("<br>payment_time : $payment_time\n");
			print("<br>payment_date : $payment_date\n");
			print("<br>response_code : $response_code\n");
			print("<br>payment_certificate : $payment_certificate\n");
			print("<br>authorisation_id : $authorisation_id\n");
			print("<br>currency_code : $currency_code\n");
			print("<br>card_number : $card_number\n");
			print("<br>cvv_flag: $cvv_flag\n");
			print("<br>cvv_response_code: $cvv_response_code\n");
			print("<br>bank_response_code: $bank_response_code\n");
			print("<br>complementary_code: $complementary_code\n");
			print("<br>complementary_info: $complementary_info\n");
			print("<br>return_context: $return_context\n");
			print("<br>caddie : $caddie\n");
			print("<br>receipt_complement: $receipt_complement\n");
			print("<br>merchant_language: $merchant_language\n");
			print("<br>language: $language\n");
			print("<br>customer_id: $customer_id\n");
			print("<br>order_id: $order_id\n");
			print("<br>customer_email: $customer_email\n");
			print("<br>customer_ip_address: $customer_ip_address\n");
			print("<br>capture_day: $capture_day\n");
			print("<br>capture_mode: $capture_mode\n");
			print("<br>data: $data\n");
			print("<br><br><hr></b></h4>");
			print ("<br><br><a href='".base_url()."/checkout/place_order'> return latinodances</a>");
			redirect('checkout/place_order');
		}
	
	}
}